﻿@{
    ViewBag.Title = "StatisticsItemMaintenanceList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}


<script type="text/javascript">
    $(function () {
        $("#ProjectID").combobox({
            editable: false, panelHeight: "auto",
            valueField: 'ID',
            textField: 'ProjectName',
            url: '/ProjectInfo/ProjectInfoList',
            onLoadSuccess: function () {
                var data = $("#ProjectID").combobox('getData');
                if (data.length > 0) {
                    $("#ProjectID").combobox('select', data[0]["ID"]);
                }
                else {
                    StatisticsItemMaintenanceList.initTable();
                }
            },
            onSelect: function (record) {
                var dataContractType = $("#ContractType").combobox('getData');
                if (dataContractType.length > 0) {
                    var contractType = $("#ContractType").combobox('getValue');
                    StatisticsItemMaintenanceList.initTable(record.ID, contractType);
                }
                else {
                    StatisticsItemMaintenanceList.initTable();
                }
            }
        });

        $('#ContractType').combobox({
            editable: false, panelHeight: "auto",
            valueField: "DictionaryName",
            textField: "DictionaryName",
            url: "/Dictionary/GetDictionaryByTypeTag?typeTag=ContractType",
            onLoadSuccess: function () {
                var data = $('#ContractType').combobox('getData');
                if (data.length > 0) {
                    $('#ContractType').combobox('select', data[0]["DictionaryName"]);
                }
                else {
                    StatisticsItemMaintenanceList.initTable();
                }
            },
            onSelect: function (record) {
                var dataProjectID = $("#ProjectID").combobox('getData');
                if (dataProjectID.length > 0) {
                    var projectID = $("#ProjectID").combobox('getValue');
                    StatisticsItemMaintenanceList.initTable(projectID, record.DictionaryName);
                }
                else {
                    StatisticsItemMaintenanceList.initTable();
                }
            }
        });
    });

    var StatisticsItemMaintenanceList = {
        initTable: function (projectID, contractType) {
            $('#statisticsItemMaintenanceDataGrid').datagrid({
                url: '/StatisticsItemMaintenance/GetStatisticsItemMaintenanceList?projectID=' + projectID + "&contractType=" + encodeURI(contractType),//一个用以从远程站点请求数据的超链接地址
                iconCls: 'icon-bricks',
                nowrap: true,//当数据长度超出列宽时将会自动截取
                autoRowHeight: false,
                striped: true,//设置为true将交替显示行背景
                collapsible: true,
                pagination: true,
                pageSize: 20,
                remoteSort: false,
                idField: 'ID',//表明该列是一个唯一列。
                singleSelect: true,
                fitColumns: true,
                toolbar: '#statisticsItemMaintenanceToolBar',
                columns:
                [[
                        { title: 'ID', field: 'ID', hidden: true },
                        { title: 'PID', field: 'PID', hidden: true },
                        { title: 'IsParent', field: 'IsParent', hidden: true },
                        { title: '父项目名称', field: 'ParentStatisticsItemName', align: 'center', width: 50 },
                        { title: '项目名称', field: 'StatisticsItemName', align: 'center', width: 50 },
                        { title: '单位', field: 'Unit', width: 100 }
                ]],
                onClickRow: function (rowIndex, rowData) {
                    if (rowData.IsParent) {
                        $('#btnModify').linkbutton('disable');
                        $('#btnModifyParent').linkbutton('enable');
                    }
                    else {
                        $('#btnModifyParent').linkbutton('disable');
                        $('#btnModify').linkbutton('enable');
                        if (rowData.PID != null) {
                            $('#btnModifyParent').linkbutton('enable');
                        }
                    }
                },
                onLoadSuccess: function (data) {
                    if (data.rows.length > 1) {
                        mergeCellsByField("statisticsItemMaintenanceDataGrid", "ParentStatisticsItemName,StatisticsItemName");
                    }
                    if (data.rows.length > 0) {
                        mergeColCells("statisticsItemMaintenanceDataGrid");
                    }
                },
                onDblClickRow: function (rowIndex, rowData) {
                    StatisticsItemMaintenanceList.Preview(rowData);
                }
            });
        },

        //新增
        Add: function () {
            var dataProjectID = $("#ProjectID").combobox('getData');
            var dataContractType = $("#ContractType").combobox('getData');
            if (dataProjectID.length > 0 && dataContractType.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                var contractType = $("#ContractType").combobox('getValue');
                StatisticsItemMaintenanceList.Mutual("新增", "/StatisticsItemMaintenance/GetStatisticsItemMaintenance?projectID=" + projectID + "&contractType=" + encodeURI(contractType), true);
            }
            else { $.EasyuiSlide("还没有项目和合同类型，不能添加"); }
        },

        //修改
        Modify: function () {
            var row = $("#statisticsItemMaintenanceDataGrid").datagrid("getSelected");
            if (row) {
                StatisticsItemMaintenanceList.Mutual("修改", "/StatisticsItemMaintenance/GetStatisticsItemMaintenance?id=" + row.ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //修改
        ModifyParent: function () {
            var row = $("#statisticsItemMaintenanceDataGrid").datagrid("getSelected");
            if (row) {
                if (row.IsParent) {
                    StatisticsItemMaintenanceList.Mutual("修改", "/StatisticsItemMaintenance/GetStatisticsItemMaintenance?id=" + row.ID);
                }
                else {
                    StatisticsItemMaintenanceList.Mutual("修改", "/StatisticsItemMaintenance/GetStatisticsItemMaintenance?id=" + row.PID);
                }
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //弹出编辑窗口
        Mutual: function (title, href, isAdd) {
            $.ModelDialog({
                title: title, href: href, width: 300, dialogid: "mdStatisticsItemMaintenance",
                handler: function () {
                    //debugger;
                    if ($('#IsParent').attr("checked") == undefined && $('#EngineeringUnit').val() == '' && $('#MoneyUnit').combobox('getValue') == '') {
                        $.EasyuiSlide("非父项时，工程量单位和金额单位不能同时为空！");
                        return;
                    }

                    if ($('#IsParent').attr("checked") == undefined && $('#EngineeringUnit').val() != '') {
                        reg = /^[a-zA-Z0-9_\u0391-\uFFE5]+(\/[a-zA-Z0-9_\u0391-\uFFE5]+)?$/;
                        if (!reg.test($('#EngineeringUnit').val())) {
                            $.EasyuiSlide("工程量单位格式不对！");
                            return;
                        }
                    }

                    $('#frmStatisticsItemMaintenance').form('submit', {
                        url: '/StatisticsItemMaintenance/IsBeRef',
                        onSubmit: function () {

                        },
                        success: function (data) {
                            if (data == "1") {
                                $.EasyuiSlide("当前统计项已经被引用，不能修改！");
                            }
                            else {
                                $('#frmStatisticsItemMaintenance').form('submit', {
                                    url: '/StatisticsItemMaintenance/SaveStatisticsItemMaintenance',
                                    onSubmit: function () {

                                    },
                                    success: function (data) {
                                        try {
                                            data = JSON.parse(data);
                                            if (data.ResultType == 0) {
                                                $('#mdStatisticsItemMaintenance').dialog("destroy");
                                                StatisticsItemMaintenanceList.Reload();
                                                $("#statisticsItemMaintenanceDataGrid").datagrid("unselectAll");
                                                $('#btnModify').linkbutton('enable');
                                                $('#btnModifyParent').linkbutton('enable');
                                                $.EasyuiSlide(data.Message);
                                            }
                                                //else if (data.ResultType == 8) {
                                                //    $.messager.alert("确认保存", "当前统计项在其他模块有引用，继续保存？", "info", function () {
                                                //        $('#frmStatisticsItemMaintenance').form('submit', {
                                                //            url: '/StatisticsItemMaintenance/SaveStatisticsItemMaintenanceAndDeleteRefData',
                                                //            onSubmit: function () {

                                                //            },
                                                //            success: function (data) {
                                                //                debugger;
                                                //                try {
                                                //                    data = JSON.parse(data);
                                                //                    if (data.ResultType == 0) {  //ResultType是在OperationResultType定义的，0表示Success成功
                                                //                        $('#mdStatisticsItemMaintenance').dialog("destroy");
                                                //                        StatisticsItemMaintenanceList.Reload();
                                                //                        $("#statisticsItemMaintenanceDataGrid").datagrid("unselectAll");
                                                //                        $('#btnModify').linkbutton('enable');
                                                //                        $('#btnModifyParent').linkbutton('enable');
                                                //                        $.EasyuiSlide(data.Message);
                                                //                    }
                                                //                    else {
                                                //                        $.EasyuiMsg(data.Message);  //弹出提示窗体
                                                //                    }
                                                //                }
                                                //                catch (e) {
                                                //                    $.Error(data);
                                                //                }
                                                //            }
                                                //        });
                                                //    });
                                                //}
                                            else {
                                                $.EasyuiMsg(data.Message);  //弹出提示窗体
                                            }
                                        }
                                        catch (e) {
                                            $.Error(data);
                                        }
                                    }
                                });
                            }
                        }
                    });

                },
                onLoad: function () { StatisticsItemMaintenanceList.FromInit(isAdd); }
            });
        },

        FromInit: function (isAdd) {
            $("#MoneyUnit").combobox({
                editable: false, panelHeight: "auto",
                data: [
                    { value: "", text: "无单位" },
                    { value: "万元", text: "万元" },
                    { value: "元", text: "元" }
                ],
                onLoadSuccess: function () {
                    if (isAdd) {
                        var data = $("#MoneyUnit").combobox('getData');
                        $("#MoneyUnit").combobox('select', "");
                    }
                }
            });

            var dataProjectID = $("#ProjectID").combobox('getData');
            var dataContractType = $("#ContractType").combobox('getData');
            if (dataProjectID.length > 0 && dataContractType.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                var contractType = $("#ContractType").combobox('getValue');
                $("#PID").combobox({
                    editable: false, panelHeight: "auto",
                    valueField: 'ID',
                    textField: 'StatisticsItemName',
                    url: '/StatisticsItemMaintenance/GetParentStatisticsItemMaintenanceList?projectID=' + projectID + '&contractType=' + encodeURI(contractType),
                    onLoadSuccess: function () {
                        if (isAdd) {
                            var data = $("#PID").combobox('getData');
                            $("#PID").combobox('select', data[0]["ID"]);
                        }
                    }
                });
            }

            if ($('#IsParent').attr("checked")) {
                $('#PID').combobox('disable');
                $('#EngineeringUnit').attr("disabled", true);
                $('#MoneyUnit').combobox('disable');
            }
        },

        AddCopy: function () {
            var dataProjectID = $("#ProjectID").combobox('getData');
            var dataContractType = $("#ContractType").combobox('getData');
            if (dataProjectID.length > 0 && dataContractType.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                var contractType = $("#ContractType").combobox('getValue');
                StatisticsItemMaintenanceList.CopyMutual("复制新增", "/StatisticsItemMaintenance/CopyStatisticsItemMaintenance");
            }
            else { $.EasyuiSlide("还没有项目和合同类型，不能添加"); }
        },

        CopyMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 300,
                handler: function () {
                    var projectID = $("#ProjectID").combobox('getValue');
                    var contractType = $("#ContractType").combobox('getValue');
                    var copyProjectID = $("#CopyProjectID").combobox('getValue');
                    var copyContractType = $("#CopyContractType").combobox('getValue');
                    if (projectID == copyProjectID && contractType == copyContractType) {
                        $.EasyuiSlide("选择复制的项目和合同类型不能与要新增的项目和合同相同！");
                        return;
                    }

                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#stage-model",
                        url: "/StatisticsItemMaintenance/SaveCopyStatisticsItemMaintenance?projectID=" + projectID + "&contractType=" + encodeURI(contractType) + "&copyProjectID=" + copyProjectID + "&copyContractType=" + encodeURI(copyContractType),
                        success: function (result) {
                            StatisticsItemMaintenanceList.Reload();
                            $("#statisticsItemMaintenanceDataGrid").datagrid("unselectAll");
                            $('#btnModify').linkbutton('enable');
                            $('#btnModifyParent').linkbutton('enable');
                        }
                    });
                },
                onLoad: function () { StatisticsItemMaintenanceList.CopyFromInit(); }
            });
        },

        CopyFromInit: function () {
            $("#CopyProjectID").combobox({
                editable: false, panelHeight: "auto",
                valueField: 'ID',
                textField: 'ProjectName',
                url: '/ProjectInfo/ProjectInfoList',
                onLoadSuccess: function () {
                    var data = $("#CopyProjectID").combobox('getData');
                    if (data.length > 0) {
                        $("#CopyProjectID").combobox('select', data[0]["ID"]);
                    }
                }
            });

            $('#CopyContractType').combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=ContractType",
                onLoadSuccess: function () {
                    var data = $('#CopyContractType').combobox('getData');
                    if (data.length > 0) {
                        $('#CopyContractType').combobox('select', data[0]["DictionaryName"]);
                    }
                }
            });
        },

        //删除
        Delete: function () {
            var arr = $('#statisticsItemMaintenanceDataGrid').datagrid('getSelections');
            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除统计项[ " + arr[0].StatisticsItemName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/StatisticsItemMaintenance/DeleteStatisticsItemMaintenance?id=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                StatisticsItemMaintenanceList.Reload();
                            }
                            else {
                                $.EasyuiSlide(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiSlide("请选择一条数据");
            }
            return false;
        },

        //向上
        Up: function () {
            StatisticsItemMaintenanceList.Order("Up");
        },

        //向下
        Down: function () {
            StatisticsItemMaintenanceList.Order("Down");
        },

        //向上或向下方法
        Order: function (order) {
            var row = $("#statisticsItemMaintenanceDataGrid").datagrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/StatisticsItemMaintenance/SetStageOrder", { id: row.ID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        StatisticsItemMaintenanceList.Reload();
                        //$("#myTreeGrid").treegrid("selectRecord", row.ID);  //重新选中对应ID的记录（此处不生效，原因不明，后续探究）
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条数据"); }
        },

        //预览
        Preview: function (rowData) {
            StatisticsItemMaintenanceList.PreviewMutual("预览", "/StatisticsItemMaintenance/PreviewStatisticsItemMaintenance?id=" + rowData.ID);
        },

        //实体操作
        PreviewMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 300,
                onLoad: function () { }
            });
        },

        //刷新
        Reload: function () {
            $("#statisticsItemMaintenanceDataGrid").datagrid("reload");
        }
    }
</script>

<div class="easyui-layout" fit="true">
    <div data-options="region:'north'" style="height:35px;">
        <table style="margin-top: 5px">
            <tr>
                <td style="width: 95px; text-align: center">项目名称</td>
                <td>
                    <input id="ProjectID" name="ProjectID" class="easyui-combobox" style="width: 180px;" />
                </td>
                <td style="width: 95px; text-align: center">合同类型</td>
                <td>
                    <input id="ContractType" name="ContractType" class="easyui-combobox" style="width: 180px;" />
                </td>
            </tr>
        </table>
    </div>

    <div data-options="region:'center'">
        <table id="statisticsItemMaintenanceDataGrid" fit="true"></table>
        <div id="statisticsItemMaintenanceToolBar">
            @Html.Raw(ViewBag.ToolBarInfo)
        </div>
    </div>
</div>
